<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Navigation as navigationModel;
use app\admin\model\Sort as sortModel;
use app\admin\model\Module as moduleModel;
use think\facade\Db;
use think\facade\View;

class Navigation extends Base{
    // 列表
    public function index(){
        $signid = input('signid'); $client = input('client'); $keyword = input('keyword'); $order = ['sequence'=>'desc','id'=>'desc'];

        // 赋值状态
        if(!is_numeric($signid)) $signid = 1;
        View::assign('signid',$signid);
        
		$rs = navigationModel::index($this->lang,$signid,$client,$keyword,$order);
		
		View::assign('list',$rs);
		View::assign('total',count($rs));
		
        return view();
    }

    // 添加编辑
    public function edit($id=0){
		$client = input('client');
        if(empty($client)) $client = 'pc';
        
        if(!request()->isPost()){
            $rs = navigationModel::find($id);
            View::assign('rs',$rs);
            $navWhere[] = ['lang','=',$this->lang]; $navWhere[] = ['signid','=',1]; $navWhere[] = ['client','find in set',$client];
            View::assign("navigation",getChildSort(Db::name('navigation')->where($navWhere)->order('sequence desc')->select()));

            // 导航属性
            $attributeWhere[] = ['tabledir','=','Navigation']; $attributeWhere[] = ['lang','=',$this->lang];
            View::assign('attribute',Db::name('attribute')->where($attributeWhere)->order('sequence desc')->select());

            // 模型列表
            $where[] = ['lang','=',$this->lang]; $where[] = ['disabled','=',1];
            $moduleList = Db::name('module')->where($where)->select();
            View::assign('module_list',$moduleList);

			return view();
        }else{
            try {
                navigationModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}

	// 单条删除
    public function delete_one(){
		$id = input('id');
        // 禁止删除含有子项的父项
        $hasChild = Db::name('navigation')->where('parentid',$id)->find();
        if(!empty($hasChild)){
            return json(['code' => 1001, 'msg' => lang('c_delete_sort_child_first'), 'data' => array('id'=>$id)]);
        }

        try {
            Db::name('navigation')->delete($id);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	/* 批量删除，返回值：
    @ error:no_check 未勾选数据
    @ success 成功
    */
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }
		
        for($i=0;$i<count($selectid);$i++){
		    $id = $selectid[$i];
            // 禁止删除含有子项的父项
            $hasChild = Db::name('navigation')->where('parentid',$id)->find();
            if(!empty($hasChild)){
                return json(['code' => 1002, 'msg' => lang('c_delete_sort_child_first'), 'data' => array('id'=>$id)]);
                break;
            }
            $deleteid[] = $id;
	    }

        try {
            Db::name('navigation')->delete($deleteid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('deleteid'=>$deleteid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    // 单个更新状态
    public function update_signid(){
        $id = input('id'); $signid = input('signid');
        // 参数不能为空
        if(empty($id) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('id'=>$id)]);
        }
        // 软删除
        if($signid == 9) $signid = time();
        
        try {
            Db::name('navigation')->where('id',$id)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    // 批量更新状态
    public function updateall_signid(){
        $selectid = input('selectid/a'); $signid = input('signid');
        // 参数错误
        if(empty($selectid) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }
        // 软删除
        if($signid == 9) $signid = time();
        
        try {
            Db::name('navigation')->where('id','in',$selectid)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
	
	// 批量更新
    public function update_all(){
		$id = input('id/a'); $title = input('title/a'); $sequence = input('sequence/a');

		foreach($id as $i => $rs){
            $data[] = ['id'=>$id[$i],'title'=>$title[$i],'sequence'=>$sequence[$i]];
        }
        
        try {
            $navigation = new navigationModel;
            $navigation->saveAll($data,true);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    // 常用导航
    public function common(){
        //模型列表
        $moduleWhere[] = ['lang','=',$this->lang]; $moduleWhere[] = ['disabled','=',1];
        $moduleList = Db::name('module')->where($moduleWhere)->select();
        View::assign('module_list',$moduleList);

        // 首页url
        $defaultLang = config('lang.default_lang');
        if(config('global.url.bind_lang') == 1){
            $domainArray = config('app.domain_bind');
            foreach ($domainArray as $domainKey => $domain){
                if($domain == $this->lang) $home_url = request()->scheme().'://'.$domainKey.'/';
                if($this->lang == $defaultLang) $home_url = request()->scheme().'://www.'.request()->rootDomain().'/';
            }
        }else{
            $home_url = '/'.$this->lang.'/';
            if($this->lang == $defaultLang) $home_url = '/';
        }
        View::assign('home_url',$home_url);

        return view();
    }

    // ico图标
    public function ico(){
        return view();
    }

    // 模型分类列表
    public function sort(){
        // 转为小写字母
        $general = new \common\General();
        $tabledir = $general->capital_to_underline(input('tabledir'));

        $keyword = input('keyword'); $order = ['sequence'=>'desc','id'=>'desc'];
        $rs = sortModel::index($tabledir,10,$this->lang,1,$keyword,$order,'navigation');

        /*判断后台分类选择方式：sort_style 1下拉，2弹窗*/
        $moduleWhere[] = ['module','=',$tabledir]; $moduleWhere[] = ['lang','=',$this->lang];
        $moduleRs = Db::name('module')->field('title,sort_style')->where($moduleWhere)->find();
        if($moduleRs['sort_style'] == 1){
            View::assign('total',count($rs));
        }else{
            View::assign('total',$rs->total());
            View::assign('lastPage',$rs->lastPage());
        }
        View::assign('list',$rs);
        View::assign('tabledir',$tabledir);
        //模型
        View::assign('module_rs',$moduleRs);

        return view();
    }

    // 模型信息列表
    public function info(){
        // 转为小写字母
        $general = new \common\General();
        $tabledir = $general->capital_to_underline(input('tabledir'));

        $sortid = input('sortid'); $keyword = input('keyword'); $order = ['addtime'=>'desc'];
        $rs = moduleModel::infoList($tabledir,$sortid,$this->lang,10,'*',$keyword,$order,1,input('client'),'navigation');

        View::assign('list',$rs);
        View::assign('total',$rs->total());
        View::assign('lastPage',$rs->lastPage());
        View::assign('tabledir',$tabledir);

        //模型
        $moduleWhere[] = ['tabledir','=',$tabledir]; $moduleWhere[] = ['lang','=',$this->lang];
        $moduleRs = Db::name('module')->field('title')->where($moduleWhere)->find();
        View::assign('module_rs',$moduleRs);

        return view();
    }
}